package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.LeescoreCategory;
import com.kede.app.service.LeescoreCategoryService;
import com.kede.app.service.dto.LeescoreCategoryQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-05-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "category管理")
@RequestMapping("/api/leescoreCategory")
public class LeescoreCategoryController {

    private final LeescoreCategoryService leescoreCategoryService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('leescoreCategory:list')")
    public void exportLeescoreCategory(HttpServletResponse response, LeescoreCategoryQueryCriteria criteria) throws IOException {
        leescoreCategoryService.download(leescoreCategoryService.queryAll(criteria), response);
    }

    /**
    * 查询category
    **/
    @GetMapping
    @Log("查询category")
    @ApiOperation("查询category")
    @PreAuthorize("@el.check('leescoreCategory:list')")
    public ResponseEntity<Object> queryLeescoreCategory(LeescoreCategoryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(leescoreCategoryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增category
    **/
    @PostMapping
    @Log("新增category")
    @ApiOperation("新增category")
    @PreAuthorize("@el.check('leescoreCategory:add')")
    public ResponseEntity<Object> createLeescoreCategory(@Validated @RequestBody LeescoreCategory resources){
        return new ResponseEntity<>(leescoreCategoryService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改category
    **/
    @PutMapping
    @Log("修改category")
    @ApiOperation("修改category")
    @PreAuthorize("@el.check('leescoreCategory:edit')")
    public ResponseEntity<Object> updateLeescoreCategory(@Validated @RequestBody LeescoreCategory resources){
        leescoreCategoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除category
    **/
    @DeleteMapping
    @Log("删除category")
    @ApiOperation("删除category")
    @PreAuthorize("@el.check('leescoreCategory:del')")
    public ResponseEntity<Object> deleteLeescoreCategory(@RequestBody Integer[] ids) {
        leescoreCategoryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("getNormalCate")
    public ResponseEntity getNormalCate(@RequestParam(value = "pid" ,defaultValue = "0") Integer pid){
        return new ResponseEntity<>(leescoreCategoryService.getNormalCate(pid),HttpStatus.OK);
    }

}